

#include "EndPromptView.h"



Scene *EndPromptView::createScene() {
	auto scene = Scene::create();
	auto layer = EndPromptView::create();
	scene->addChild(layer);
	return scene;
}

bool EndPromptView::init() {
	if (!Layer::init()) {
		return false;
	}

	win_height = Director::getInstance()->getWinSize().height;
	win_width = Director::getInstance()->getWinSize().width;

	UI = cocostudio::GUIReader::getInstance()->widgetFromJsonFile(
				"StartPromptScreen_1.ExportJson");
	   UI->setAnchorPoint(Vec2::ZERO);
		UI->setPosition(Point(0, win_height - UI->getContentSize().height));
		this->addChild(UI);


	    promptFrame = (Widget*)Helper::seekWidgetByName(UI, "prompt_frame");
		promptFrame->setAnchorPoint(Vec2::ZERO);

		Color4B c;
		c.a = 255;
		c.r = 140;
		c.g = 91;
		c.b = 0;
		promptTitle = Label::create("", "Arial", 18);
		promptTitle->setTextColor(c);
		promptTitle->setPosition(Point(promptFrame->getContentSize().width/2, promptFrame->getContentSize().height/2));
	    promptFrame->addChild(promptTitle);

	return true;
}

void EndPromptView::dismiss(bool success){

    this->removeFromParent();
    if(success){
    	Director::getInstance()->replaceScene(ShowWinScreen::createScene());
    }else{
    	Director::getInstance()->replaceScene(ShowFailScreen::createScene());
    }
}

std::string EndPromptView::getTitle(bool success){

	if(success){
	__String *hint = __String::createWithFormat("EndPromptTitle_success");
		return LocalizedStringGetter::getStringByKey(hint->getCString());
	}else{
		__String *hint = __String::createWithFormat("EndPromptTitle_fail");
				return LocalizedStringGetter::getStringByKey(hint->getCString());
	}
}


void EndPromptView::show(bool success){
	mSuccess = success;
	promptTitle->setString(getTitle(success));
    MoveTo* moveTo = MoveTo::create(0.3f, Point(0,
    		    UI->getContentSize().height -   win_height/2 - promptFrame->getContentSize().height/2));

    		JumpBy* jumpBy = JumpBy::create(0.5f, Point(0, 0), 15, 1);

    		MoveTo* moveStay = MoveTo::create(1.5f, Point(0,
    				 UI->getContentSize().height -   win_height/2 - promptFrame->getContentSize().height/2));

    		MoveTo* moveBack = MoveTo::create(0.3f, Point(0,
    				UI->getContentSize().height	-	promptFrame->getContentSize().height));

    		auto callbackFunc = [&](){
    	       dismiss(mSuccess);
    		    };
    		    CallFunc* callFunc = CallFunc::create(callbackFunc);


    		Action* action = Sequence::create(moveTo, jumpBy, moveStay, moveBack, callFunc, NULL);
    		promptFrame->runAction(action);
}


